using UnityEngine;
using System.Collections;

//goes on healthbar, will need to link up to health eventually

public class scalehealth : MonoBehaviour {
	
	Transform HealthBar;
	private float MaxHealth = 100;
	private float MaxBar = 0.2f;
	float Health = 100;
	Vector3 healthbar;
	
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if(transform.localScale.x >= 0.2f){
	    
	        transform.renderer.material.color = Color.green;
	    }
	    if(transform.localScale.x <= 0.15f)
	    {
	        transform.renderer.material.color = Color.yellow;
	    }
	    if(transform.localScale.x <= 0.05f)
	    {
	        transform.renderer.material.color = Color.red;
	    }
	     if(transform.localScale.x <= 0.00f)
	    {
			    
	
	    //Debug.Log("im dead");
	       Destroy(gameObject);
	    }
		
	}
	void OnTriggerEnter(Collider other)
	{
	   
	 	if(other.gameObject.tag == "enemybullet"){
	 	 StartCoroutine(LerpScale(1));
	 	
	 	}
	 
	}
	 
	 IEnumerator LerpScale(float time)
	{
	   Vector3 originalScale = transform.localScale;
	  Vector3 targetScale = originalScale - new Vector3(.05f, 0.0f, 0.0f);
	   float originalTime = time;
	 
	   while (time > 1.9f)
	   {
	      time -= Time.deltaTime;
	 
	      transform.localScale = Vector3.Lerp(targetScale, originalScale, time / originalTime);
	 	//   yield return new WaitForSeconds(time);
	    //  yield return new WaitForSeconds(9);
			yield return time;
	   }
	}
	
	
}
